//
//  LoginViewController.m
//  babyMemoryPro
//
//  Created by student on 15-7-1.
//  Copyright (c) 2015年 niit. All rights reserved.
//

#import "LoginViewController.h"
#import "SignUpViewController.h"
#import  "LolitaFunctions.h"
#import "SLideZoomMenuController.h"
#import "MainViewController.h"
#import "LeftViewController.h"
#import "AppDelegate.h"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)buttonLoginTouched:(id)sender {
    NSString *errorMessage = [self checkUserDetails];
    if (![errorMessage length]){
        
        NSCharacterSet *whiteNewChars = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *username = [_textFieldUsername.text stringByTrimmingCharactersInSet:whiteNewChars];
        NSString *password = [_textFieldPassword.text stringByTrimmingCharactersInSet:whiteNewChars];
        
        
        [AVUser logInWithUsernameInBackground:username password:password
                                        block:^(AVUser *user , NSError *error){
                                            //[_LolitaFunctions hideLoadingView:viewLoading];
                                            if (user != nil){
                                                AppDelegate *app = [UIApplication sharedApplication].delegate;
                                                MainViewController *mainVC = [MainViewController new];
                                                LeftViewController *leftVC = [[LeftViewController alloc] init];
                                                leftVC.selectedIndex = 0;
                                                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainVC];
                                                SLideZoomMenuController *slideZoomMenu = [[SLideZoomMenuController alloc] initWithRootController:nav];
                                                slideZoomMenu.leftViewController = leftVC;
                                               app.window.rootViewController = slideZoomMenu;;
                                                app.slider = slideZoomMenu;
                                                NSLog(@"登陆成功");
                                            }else{
                                                NSString *errMsg = [error userInfo][@"error"];
                                                [[[UIAlertView alloc] initWithTitle:@"登录失败"
                                                                            message:[errMsg capitalizedString]
                                                                           delegate:nil
                                                                  cancelButtonTitle:@"确定"
                                                                  otherButtonTitles:nil, nil] show];
                                            }
                                        }];
        
        
        
    }else{
        [[[UIAlertView alloc] initWithTitle:@"登录失败"
                                    message:errorMessage
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil, nil] show];
    }
}

- (IBAction)signUpPush:(id)sender {
    SignUpViewController *signUp=[[SignUpViewController alloc]init];
    [self.navigationController pushViewController:signUp animated:YES];
}

//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
//}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [_textFieldUsername setTextColor:[UIColor whiteColor]];
    [_textFieldPassword setTextColor:[UIColor whiteColor]];
}
#pragma mark - check user details
-(NSString *)checkUserDetails {
    NSCharacterSet *whiteNewChars = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    
    NSString *username = [_textFieldUsername.text stringByTrimmingCharactersInSet:whiteNewChars];
    NSString *password = [_textFieldPassword.text stringByTrimmingCharactersInSet:whiteNewChars];
    
    NSString *message = @"";
    
    if ([username length] < 3){
        message = [NSString stringWithFormat:@"用户名太短啦"];
    }
    if ([password length] < 6){
        if ([message length]) message = [NSString stringWithFormat:@"%@, ",message];
        message = [NSString stringWithFormat:@"%@请谨慎设置密码",message];
    }
    
    return message;
}
#pragma mark - UITextField Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField == _textFieldUsername)
        [_textFieldUsername becomeFirstResponder];
    else{
        [textField resignFirstResponder];
        [self buttonLoginTouched:_btnLogin];
    }
    
    return (textField == _textFieldPassword);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UIView *viewTouched = [[touches anyObject] view];
    if(![viewTouched isKindOfClass:[UITextField class]]){
        [_textFieldUsername resignFirstResponder];
        [_textFieldPassword resignFirstResponder];
    }
}
@end
